<?php
/**
 * Created by PhpStorm.
 * User: yxs
 * Date: 2017/6/28
 * Time: 14:41
 */

namespace app\shop\controller;

use icontroller\wechat\AController;
use library\common\str;
use model\ShopCollect;
use model\ShopUserCart;

use model\ShopGoods;
use model\ShopGoodsCategory;
use model\ShopStore;
use model\ShopGoodsVariant;
use think\Cookie;

class Goods extends AController
{
    // <editor-fold defaultstate="collapsed" desc="店铺商品列表">
    public function index()
    {
        $store_id = input('store_id');
        $category = input('category');

        $where = [
            "flag_delete" => 0,
            "flag_putaway" => 1,
            "store_id" => $store_id,
        ];
        if (!empty($category)) {
            $where["category_tree"] = $category;
        }
        //获取店铺信息
        $shop = ShopStore::get($store_id);
        $collect = ShopCollect::get(["account_id" => $this->account_id, "store_id" => $store_id]);
        if ($collect) {
            $is_collect = 1;
        } else {
            $is_collect = 0;
        }
        $total_price = $this->get_total_price($store_id);

        if ($shop["flag_lock"] == 1 || !str::time_quantum($shop["bizhours_start"], $shop["bizhours_end"])) {
            $shop["is_work"] = 0; //歇业中
        } else {
            $shop["is_work"] = 1; //营业中
        }
 //       $point = cookie("point");
//        $lat = null;
//        $lng = null;
//        if (!empty($point)) {
//            $lat = $point["lat"];
//            $lng = $point["lng"];
//        }
        $lat = $this->account["current_lat"];
        $lng = $this->account["current_lng"];
        $distance = 0;
        if (!empty($lat) && !empty($lng) && !empty($shop["latitude"]) && !empty($shop["longitude"])) {
            $distance = str::getDistance($lat, $lng, $shop["latitude"], $shop["longitude"]);
        }
        $this->assign("distance", $distance);
        $this->assign("is_collect", $is_collect);
        $this->assign("total_price", number_format($total_price, 2, '.', ''));
        $this->assign("shop", $shop);
        $this->assign("store_id", $store_id);
        $this->assign("category", $category);
        return view();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="获取商品列表">
    public function get_list()
    {
        $category = input('category');
        $store_id = input('store_id');
        $where = [
            "flag_delete" => 0,
            "flag_putaway" => 1,
            "store_id" => $store_id,
        ];
        if (!empty($category)) {
            $where["category_tree"] = $category;
        }
        $mo = new ShopGoods();
        $mo_sv = new ShopGoodsVariant();
        $su_c = new ShopUserCart();
        $list = $mo->where($where)->select();
        foreach ($list as &$v) {
            $v["quantity"] = 0;
            $c_list = ShopUserCart::all(["account_id" => $this->account_id, "goods_id" => $v->id]);
            $cv_count = $su_c->where(["account_id" => $this->account_id, "goods_id" => $v->id, "variant_id" => [">", 0]])->count();
            if (!empty($c_list)) {
                foreach ($c_list as $c) {
                    if ($c->variant_id > 0) {
                        $variant = ShopGoodsVariant::get($c->variant_id);
                        if ($variant && $variant->flag_delete != 1) {
                            $v["quantity"] += $c->quantity;
                        }
                        if ($cv_count == 1) {
                            $v["v_price"] = $variant->price;
                        }
                    } else {
                        $v["quantity"] += $c->quantity;
                    }
                }
            }
            if (empty($cv_count) || $cv_count > 1) {
                $v["v_price"] = $v->price;
            }

            //获取商品规格
            $v_count = $mo_sv->where(["goods_id" => $v->id, "flag_delete" => 0])->count();
            if ($v_count > 0) {
                $v["v_flag"] = 1;
            } else {
                $v["v_flag"] = 0;
            }
        }
        $this->success("获取数据成功!", null, $list);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="获取分类列表">
    public function get_clist()
    {
        $store_id = input('store_id');
        //获取商品分类
        $mo = new ShopGoods();
        $ct_mo = new ShopGoodsCategory();
        $category_tree = $mo->where(["flag_putaway" => 1, "flag_delete" => 0, "store_id" => $store_id])->column("category_tree");
        $c_list = $ct_mo->where(["id" => ["in", $category_tree], "store_id" => $store_id])->field("name,id,isdefault")->select();
        $this->success("获取数据成功!", null, $c_list);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="获取对应店铺的购物车商品总额">
    private function get_total_price($store_id)
    {
        $total_price = 0;
        if (!empty($store_id)) {
            $mo = new ShopGoods();
            $mo_sv = new ShopGoodsVariant();
            $where = [
                "flag_delete" => 0,
                "flag_putaway" => 1,
                "store_id" => $store_id,
            ];
            $list = $mo->where($where)->field("id,price")->select();

            $total_price = 0;
            foreach ($list as &$v) {
                $c_list = ShopUserCart::all(["account_id" => $this->account_id, "goods_id" => $v->id]);
                if (!empty($c_list)) {
                    foreach ($c_list as $c) {
                        if ($c->variant_id > 0) {
                            $price = $mo_sv->where(["id" => $c->variant_id, "flag_delete" => 0])->value("price");
                            if ($price) {
                                $total_price += bcmul($price, $c->quantity, 2);
                            }
                        } else {
                            $total_price += bcmul($v->price, $c->quantity, 2);
                        }
                    }
                }
            }
        }
        return number_format($total_price, 2, '.', '');
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="商品详情">
    public function detail()
    {
        $id = input("id");
        $info = ShopGoods::get($id);
        $c_list = ShopUserCart::all(["account_id" => $this->account_id, "goods_id" => $info->id]);
        $su_c = new ShopUserCart();
        $cv_count = $su_c->where(["account_id" => $this->account_id, "goods_id" => $id, "variant_id" => [">", 0]])->count();
        $mo_sv = new ShopGoodsVariant();
        $info["quantity"] = 0;
        if (!empty($c_list)) {
            foreach ($c_list as $c) {
                if ($c->variant_id > 0) {
                    $variant = ShopGoodsVariant::get($c->variant_id);
                    if ($variant && $variant->flag_delete != 1) {
                        $info["quantity"] += $c->quantity;
                    }
                    if ($cv_count == 1) {
                        $info["v_price"] = $variant->price;
                    }
                } else {
                    $info["quantity"] += $c->quantity;
                }
            }
        }
        if (empty($cv_count) || $cv_count > 1) {
            $info["v_price"] = $info->price;
        }
        //获取商品规格
        $v_count = $mo_sv->where(["goods_id" => $info->id, "flag_delete" => 0])->count();
        if ($v_count > 0) {
            $info["v_flag"] = 1;
        } else {
            $info["v_flag"] = 0;
        }

        $total_price = $this->get_total_price($info->store_id);
        //获取店铺信息
        $shop = ShopStore::get($info->store_id);
        if ($shop["flag_lock"] == 1 || !str::time_quantum($shop["bizhours_start"], $shop["bizhours_end"])) {
            $info["is_work"] = 0; //歇业中
        } else {
            $info["is_work"] = 1; //营业中
        }
        $this->assign("store_id", $info->store_id);
        $this->assign("info", $info);

        $this->assign("slide_image", explode(",", $info->slide_image));
        $this->assign("total_price", $total_price);
        return view();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="获取商品规格">
    public function get_variant()
    {
        $id = input("id");
        $mo_sv = new ShopGoodsVariant();
        $list = $mo_sv->where(["goods_id" => $id, "flag_delete" => 0])->select();
        $this->success("获取规格成功", null, $list);
    }
    // </editor-fold>
}